-- bank_exchange
-- created by panyl
-- 冈布奥事件：银行兑换

-- 购买商场物品
EventMgr.register("bank_exchange", event.BANK_EXCHANGE_DONE, function(para)
    local dungeonId = DungeonM.getDungeonId();
    local rate = para["gem"] * 3;
    local rand = RandomFactoryM.myRandom(ME.user) % 100;

    -- 次数
    local times = SlimeEventM.getProgress("bank_exchange");

    -- 钻石VIP，必定获得礼盒的属性
    local props = PropM.fetchProps(ME.user, "bank_gift");
    if #props > 0 then
        rand = 0;
    end

    if times > 0 and rand >= rate then
        -- gem * 2 %的概率触发
        return;
    end

    -- 获取主迷宫id
    dungeonId = DungeonAreaM.getParentId(dungeonId);

    -- 统计次数
    SlimeEventM.addProgress("bank_exchange");

    local arr = SlimeEventM.getIdsByRule("bank_exchange");
    for index = 1, #arr do
        SlimeEventM.triggerEvent(arr[index], {["dungeonId"] = dungeonId, });
    end

    -- 钻石VIP：必定获得礼盒
    local prop = PropM.combine(ME.user, "bank_gift", 1);
    if prop[3] > 0 then
        -- 触发特技发动事件
        EventMgr.fire(event.SPECIAL_SKILL_TRIGGER, { propId = prop[1] });
    end
end);

return {
    checkEvent = function(eventId, para)
        local dungeonId = para.dungeonId;
        if not dungeonId then
            return false;
        end

        local cond = SlimeEventM.query(eventId, "condition");
        if cond ~= dungeonId then
            return false;
        end

        return true;
    end,
};

